package com.zd.miko.tally.CustomView;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.ShapeDrawable;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;

import com.zd.miko.tally.R;
import com.zd.miko.tally.Utils.Utils;

import java.util.List;

/**
 * Created by Miko on 2018/3/28.
 */

public class CustomKeyboardView extends KeyboardView {

    public CustomKeyboardView(Context context) {
        this(context, null);
    }

    public CustomKeyboardView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomKeyboardView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        inflate(context, R.layout.custom_keyboead, null);
        options.inJustDecodeBounds = true;
        bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_key_delete, options);
    }

    Bitmap bitmap;
    BitmapFactory.Options options = new BitmapFactory.Options();

    ShapeDrawable completeDrawable = new ShapeDrawable();


    @Override
    public void onDraw(Canvas canvas) {
        // super.onDraw(canvas);

        Context context = this.getContext();
        List<Keyboard.Key> keys = getKeyboard().getKeys();
        for (Keyboard.Key key : keys) {
            if (key.codes[0] == -99) {
                completeDrawable.getPaint().setColor(ContextCompat.getColor(getContext(), R.color.editToolbar));
                completeDrawable.getPaint().setAntiAlias(true);
                completeDrawable.getPaint().setStyle(Paint.Style.FILL);
                ;
                completeDrawable.setBounds(key.x, key.y, key.x + key.width, key.y + key.height);
                completeDrawable.draw(canvas);
            } else {
                completeDrawable.getPaint().setColor(Color.WHITE);
                completeDrawable.getPaint().setAntiAlias(true);
                completeDrawable.getPaint().setStyle(Paint.Style.FILL);
                ;
                completeDrawable.setBounds(key.x, key.y, key.x + key.width, key.y + key.height);
                completeDrawable.draw(canvas);
            }

            Paint paint = new Paint();
            paint.setAntiAlias(true);
            paint.setDither(true);
            paint.setTextAlign(Paint.Align.CENTER);
            paint.setTextSize(48);
            paint.setColor(Color.BLACK);

            if (key.label != null) {
                Rect bounds = new Rect();
                paint.getTextBounds(key.label.toString(), 0, key.label.toString().length(), bounds);
                Paint.FontMetricsInt fontMetrics = paint.getFontMetricsInt();
                int baseline = (key.height - fontMetrics.bottom + fontMetrics.top) / 2 - fontMetrics.top;
                canvas.drawText(key.label.toString(), key.x + (key.width / 2),
                        key.y + baseline, paint);
                bounds = null;

            } else {

                int height = options.outHeight;
                int width = options.outWidth;

                int b = height / width;

                int w = Utils.dpToPx(20);
                int h = w * b;

                key.icon.setDither(true);

                key.icon.setBounds(key.x + (key.width - w) / 2, key.y + (key.height - h) / 2, key.x + (key.width - w) / 2 + w, key.y + (key.height - h) / 2 + h);
                key.icon.draw(canvas);
            }
        }
    }
}
